package com.gxstnu.controller;

import com.github.pagehelper.PageInfo;
import com.gxstnu.common.Result;
import com.gxstnu.entity.Task;
import com.gxstnu.service.TaskService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/task")
public class TaskController {

    @Resource
    TaskService taskService;

    /**
     * 获取任务列表
     */
    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       Task task) {
        PageInfo<Task> taskPageInfo = taskService.selectPage(pageNum, pageSize, task);
        return Result.success(taskPageInfo);
    }

    /**
     * 获取任务详情
     */
    @GetMapping("/detail")
    public Result detail(@RequestParam Integer taskId) {
        Task task = taskService.selectById(taskId);
        return Result.success(task);
    }

    /**
     * 发布任务
     */
    @PostMapping("/publish")
    public Result publish(@RequestBody Task task) {
        try {
            taskService.publishTask(task);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 接受任务
     */
    @PostMapping("/take")
    public Result take(@RequestBody Map<String, Object> params) {
        try {
            Integer taskId = (Integer) params.get("taskId");
            // 这里应该从登录用户中获取接单者信息
            // 为了演示，这里使用固定值
            Integer accepterId = 1;
            String accepter = "测试用户";
            
            taskService.takeTask(taskId, accepterId, accepter);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 完成任务
     */
    @PostMapping("/complete")
    public Result complete(@RequestBody Map<String, Object> params) {
        try {
            Integer taskId = (Integer) params.get("taskId");
            taskService.completeTask(taskId);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 取消任务
     */
    @PostMapping("/cancel")
    public Result cancel(@RequestBody Map<String, Object> params) {
        try {
            Integer taskId = (Integer) params.get("taskId");
            taskService.cancelTask(taskId);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 添加任务
     */
    @PostMapping("/add")
    public Result add(@RequestBody Task task) {
        taskService.add(task);
        return Result.success();
    }

    /**
     * 更新任务
     */
    @PutMapping("/update")
    public Result update(@RequestBody Task task) {
        taskService.update(task);
        return Result.success();
    }

    /**
     * 删除任务
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        taskService.deleteById(id);
        return Result.success();
    }
}